package com.hndzyu.yd.vip;

import android.annotation.SuppressLint;
import android.content.Context;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.request.RequestOptions;
import com.hndzyu.yd.R;
import com.hndzyu.yd.bean.PDDDetail;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Created by YJ on 2018/10/23.
 */
public class PDDRelatedAdapter extends RecyclerView.Adapter<PDDRelatedAdapter.ViewHolder> {

    private Context mContext;
    private List<PDDDetail.ListBean> mData = new ArrayList<>();
    private PDDSourceListener listener;
    public PDDRelatedAdapter(Context context) {
        mContext = context;
    }
    public void setListener(PDDSourceListener listener){
        this.listener=listener;
    }
    public void updateData(List<PDDDetail.ListBean> dataSet) {
        if (dataSet != null && !dataSet.isEmpty()) {
            mData = dataSet;
            notifyDataSetChanged();
        }
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(mContext).inflate(R.layout.item_pdd_related, parent, false);
        return new ViewHolder(view);
    }

    @SuppressLint("SetTextI18n")
    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
        final PDDDetail.ListBean dataBean = mData.get(position);
        Glide.with(mContext).load(dataBean.getGoods_thumbnail_url()).apply(new RequestOptions().error(R.mipmap.logo)).into(holder.reImg);
        holder.tvReName.setText(dataBean.getGoods_name());
        holder.tvRePrice.setText((double)dataBean.getMin_group_price()/100+"元");
        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                listener.click(dataBean);
            }
        });
    }

    @Override
    public int getItemCount() {
        return mData.size();
    }

    public class ViewHolder extends RecyclerView.ViewHolder {
        @BindView(R.id.re_img)
        ImageView reImg;
        @BindView(R.id.tv_re_name)
        TextView tvReName;
        @BindView(R.id.tv_re_price)
        TextView tvRePrice;
        public ViewHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }
    }
    public interface PDDSourceListener{
        void click(PDDDetail.ListBean item);
    }
}
